home *** CD-ROM | disk | FTP | other *** search
- Path: ix.netcom.com!netnews
- From: miker3@ix.netcom.com (Mike Rubenstein)
- Newsgroups: comp.lang.c
- Subject: Re: is this string a number?
- Date: Mon, 15 Jan 1996 01:32:06 GMT
- Organization: Netcom
- Message-ID: <30f9ac87.195826304@nntp.ix.netcom.com>
- References: <4dbogk$763@jupiter.planet.net>
- NNTP-Posting-Host: ix-dc13-06.ix.netcom.com
- X-NETCOM-Date: Sun Jan 14 5:32:02 PM PST 1996
- X-Newsreader: Forte Agent .99c/16.141
-
- Chris Kemp <chrisk@paladn.com> wrote:
-
- |>I am inputting a string from the keyboard from a user, and
- |>intend to use the atol or strtol functions to convert the
- |>string to a number.
- |>
- |>But first I must make sure the user provided a legitimate
- |>number (not line 1234B).
- |>
- |>Is there a library function which will make this determination
- |>for me, or must I make up one of my own with somthing like
- |>strtok?
-
- Use strtol() and supply a non-null second argument. When strtol()
- returns, the pointer will point to the first character that was not
- converted. Example:
-
- #include <stdlib.h>
-
- char *str;
- char *nstr;
- long l;
-
- /* ... */
-
- l = strtol(str, &nstr, 10);
- if (nstr == str || *nstr != '\0')
- {
- /* str was empty or there is an illegal character */
- }
-
-
- Michael M Rubenstein
-